<template> 
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float:right"
          type="primary"
          @click="handleSearchList()"
          size="small">
          查询搜索
        </el-button>
        <el-button
          style="float:right;margin-right: 15px"
          @click="handleResetSearch()"
          size="small">
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="管理员名称搜索：">
            <el-input v-model="listQuery.username" class="input-width" placeholder="请输入管理员名称"></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>用户列表</span>
    </el-card>
    <div class="table-container">
      <el-table ref="orderTable"
            :data="list"
            style="width: 100%;"
            @selection-change="handleSelectionChange"
            v-loading="listLoading" border>
        <el-table-column type="selection" width="60" align="center"></el-table-column>
        <el-table-column label="编号" width="80" align="center">
          <template slot-scope="scope">{{scope.row.id}}</template>
        </el-table-column>
        <el-table-column label="用户名称" align="center" width="180">
          <template slot-scope="scope">{{scope.row.username}}</template>
        </el-table-column>
        <el-table-column label="邮箱" width="180" align="center">
          <template slot-scope="scope">{{scope.row.email}}</template>
        </el-table-column>
        <el-table-column label="提交时间" width="180" align="center">
          <template slot-scope="scope">{{scope.row.createTime | formatCreateTime}}</template>
        </el-table-column>
         <el-table-column label="用户备注" width="450" align="center">
          <template slot-scope="scope">{{scope.row.note}}</template>
        </el-table-column>


        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleViewUser(scope.$index, scope.row)"
            >查看用户</el-button>
            <el-button
              size="mini"
              @click="changeUser(scope.$index, scope.row)"
              >修改用户信息</el-button>


            <el-button
              size="mini"
              type="danger"
              @click="handleDeleteUser(scope.$index, scope.row)"
              >删除用户</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="batch-operate-container">
      <el-select
        size="small"
        v-model="operateType">
        <el-option
          v-for="item in operateOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      <el-button
        style="margin-left: 20px"
        class="search-button"
        @click="handleBatchOperate()"
        type="primary"
        size="small">
        确定
      </el-button>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[5,10,15]"
        :total="total">
      </el-pagination>
    </div>


    <el-dialog
      title="用户信息"
      :visible.sync="userMessagedialogVisible" width="30%">
      <el-form :model="userMessage" label-width="50px" class="demo-ruleForm">
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="userMessage.email" disabled autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="username">
          <el-input  v-model="userMessage.username" disabled autocomplete="off"></el-input>
        </el-form-item>
        <!-- <el-form-item label="密码" prop="password">
          <el-input type="text" v-model.number="userMessage.password"></el-input>
        </el-form-item> -->
        <el-form-item label="备注" prop="note">
          <el-input type="textarea" disabled v-model="userMessage.note"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="userMessagedialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleuserMessageConfirm">确 定</el-button>
      </span>
    </el-dialog>



    <el-dialog
      title="修改信息"
      :visible.sync="changeDialogVisible" width="30%">
      <el-form :model="userMessage" label-width="50px" class="demo-ruleForm">
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="userMessage.email"  autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="username">
          <el-input  v-model="userMessage.username"  autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="text" v-model.number="userMessage.password"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="note">
          <el-input type="textarea"  v-model="userMessage.note"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="changeDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="changeUserConfirm">确 定</el-button>
      </span>
    </el-dialog>

    <logistics-dialog v-model="logisticsDialogVisible"></logistics-dialog>
  </div>
</template>
<script>
  import {fetchList,userMessage,} from '@/api/order'
   import { userList ,upDateUser,deleteUser} from '@/api/user'
  import {formatDate} from '@/utils/date';
  import LogisticsDialog from '@/views/oms/order/components/logisticsDialog';
  const defaultListQuery = {
    pageNum: 1,
    pageSize: 10,
    orderSn: null,
    receiverKeyword: null,
    status: null,
    orderType: null,
    sourceType: null,
    createTime: null,
    username: ''
  };
  export default {
    name: "orderList",
    components:{LogisticsDialog},
    data() {
      return {
        userMessagedialogVisible: false,
        changeDialogVisible : false,

        listQuery: Object.assign({}, defaultListQuery),
        listLoading: true,
        list: null,
        total: null,
        operateType: 0,
        multipleSelection: [],
        userMessage:{

        },
        statusOptions: [
          {
            label: '待付款',
            value: 0
          },
          {
            label: '待发货',
            value: 1
          },
          {
            label: '已发货',
            value: 2
          },
          {
            label: '已完成',
            value: 3
          },
          {
            label: '已关闭',
            value: 4
          }
        ],
        orderTypeOptions: [
          {
            label: '正常订单',
            value: 0
          },
          {
            label: '秒杀订单',
            value: 1
          }
        ],
        sourceTypeOptions: [
          {
            label: 'PC订单',
            value: 0
          },
          {
            label: 'APP订单',
            value: 1
          }
        ],
        operateOptions: [
          {
            label: "批量删除",
            value: 0
          },

        ],
        logisticsDialogVisible:false
      }
    },
    created() {
      this.getList();
    },
    filters: {
      formatCreateTime(time) {
        let date = new Date(time);
        return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
      },
      formatPayType(value) {
        if (value === 1) {
          return '支付宝';
        } else if (value === 2) {
          return '微信';
        } else {
          return '未支付';
        }
      },
      formatSourceType(value) {
        if (value === 1) {
          return 'APP订单';
        } else {
          return 'PC订单';
        }
      },
      formatStatus(value) {
        if (value === 1) {
          return '待发货';
        } else if (value === 2) {
          return '已发货';
        } else if (value === 3) {
          return '已完成';
        } else if (value === 4) {
          return '已关闭';
        } else if (value === 5) {
          return '无效订单';
        } else {
          return '待付款';
        }
      },
    },
    methods: {
      handleResetSearch() {
        this.listQuery = Object.assign({}, defaultListQuery);
      },
      handleSearchList() {
        console.log(this.listQuery)
        if (!this.listQuery.username) {
          return  this.$message({
              message: '请输入搜索条件',
              type: 'warning',
              duration: 1000
            });
        }
        this.listQuery.name = this.listQuery.username
        this.listQuery.pageNum = 1;
        this.getList();
      },
      handleSelectionChange(val){
        this.multipleSelection = val;
        console.log(val)
      },
      handleViewUser(index, row){
        // 查看用户
        this.userMessagedialogVisible = true
        this.userMessage = {}
        this.userMessage = row
        console.log(this.userMessage)
      },
      changeUser(index, row){
        // 修改用户
        this.changeDialogVisible =true
        this.userMessage = {}
        this.userMessage = row
      },


      handleDeleteUser(index, row){
        // 删除用户
        let deleteUserArr = []
        deleteUserArr.push(row.id)
        // console.log(deleteUserArr )
        this.$confirm(`确认删除${row.username}?`, '删除用户', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {

          deleteUser(deleteUserArr).then(response => {
            this.this.getList()
             this.$message({
                type: 'success',
                message: '删除成功!'
              });

          });



        }).catch((err) => {
          console.log(err)
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      handleBatchOperate(){
        if(this.multipleSelection==null||this.multipleSelection.length<1){
          this.$message({
            message: '请选择要删除的用户',
            type: 'warning',
            duration: 1000
          });
          return;
        }
          //删除订单
        let ids=[];
        for(let i=0;i<this.multipleSelection.length;i++){
          ids.push(this.multipleSelection[i].id);
        }
        this.delete(ids);
      },
      handleSizeChange(val){
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val){
        this.listQuery.pageNum = val;
        this.getList();
      },
      handleuserMessageConfirm() {
        this.userMessagedialogVisible = false
        // if (this.userMessage.content == null || this.userMessage.content === '') {
        //   this.$message({
        //     message: '操作备注不能为空',
        //     type: 'warning',
        //     duration: 1000
        //   });
        //   return;
        // }
        // let params = new URLSearchParams();
        // params.append('ids', this.userMessage.orderIds);
        // params.append('note', this.userMessage.content);
        // userMessage(params).then(response=>{
        //   this.userMessage.orderIds=[];
        //   this.userMessage.dialogVisible=false;
        //   this.getList();
        //   this.$message({
        //     message: '修改成功',
        //     type: 'success',
        //     duration: 1000
        //   });
        // });
      },

      changeUserConfirm() {
        this.listLoading = true
        // this.changeDialogVisible  = true
        upDateUser(this.userMessage,this.userMessage.id).then(response => {
          // cossole.log(response)
          this.changeDialogVisible  = false
          this.getList()

        });


      },
      getList(userName) {

        this.listLoading = true;
        userList(this.listQuery).then(response => {
          this.listLoading = false;
          this.list = response.data.list;
          this.total = response.data.total;
        });
      },
      delete(ids){
        this.$confirm('是否要进行该删除操作?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let params = []
          // params.append("ids",ids);
          console.log('ids',ids)
          deleteUser(ids).then(response=>{
            this.$message({
              message: '删除成功！',
              type: 'success',
              duration: 1000
            });
            this.getList();
          });
        })
      },
      covertOrder(order){
        let address=order.receiverProvince+order.receiverCity+order.receiverRegion+order.receiverDetailAddress;
        let listItem={
          orderId:order.id,
          orderSn:order.orderSn,
          receiverName:order.receiverName,
          receiverPhone:order.receiverPhone,
          receiverPostCode:order.receiverPostCode,
          address:address,
          deliveryCompany:null,
          deliverySn:null
        };
        return listItem;
      }
    }
  }
</script>
<style scoped>
  .input-width {
    width: 203px;
  }
</style>


